/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#ifndef MK_HTTP_MEDIA_SEERVER_H
#define MK_HTTP_MEDIA_SEERVER_H
#include "MkHttpApi/MkHttpApi.h"
#include "MkMedia/MkMediaSource.h"
#include "MkMedia/MkMediaConsumer.h"

typedef MkFunction<Uint32(const MkHttpMethod& Method, const MkString& Path, const MkMapString& MapQuery, const Json::Value& JsonReq, const MkHttpResponseJsonCb& cb)> MkHttpJsonRequestCallback;
typedef MkFunction<void(CMkHttpSession* pSession)> MkWsAccessedCallback;
class MkApi CMkHttpMediaServer : public CMkHttpApi
{
public:
    CMkHttpMediaServer(CMkEventPool* pEventPool, CMkTimer* pTimer, const MkHttpJsonRequestCallback& HttpReqCb, const MkWsAccessedCallback& WsAccessedCb, const MkMediaCallbacks& MediaCallbacks);
    virtual ~CMkHttpMediaServer();
protected:
private:
    CMkTcpSession* CreateNewSession(CMkEvent *pEvent, CMkTimer* pTimer) override;
    Uint32 OnSessionEventRemoved(CMkTcpSession* pSession) override;
    Uint32 OnRecvRequest(CMkHttpSession* pSession, const CMkHttpRequest& Request) override;
    Uint32 OnRequestJsonParse(const MkHttpMethod& Method, const MkString& Path, const MkMapString& MapQuery, const Json::Value& JsonReq, const MkHttpResponseJsonCb& cb) override;
    Uint32 OnWebsocketRecved(CMkHttpSession* pSession, const MkString& Message) override;
    Uint32 OnWebSockekAccessed(CMkHttpSession* pSession) override;
private:
    MkMediaCallbacks                                m_MediaCbs;
    MkHttpJsonRequestCallback                       m_HttpRequestCallBack;
    MkWsAccessedCallback                            m_WsAccessedCb;

    MkMap<Uint64, MkListCacheStreamPacket>          m_MapStreamCache;  //key http session id
};


#endif